package locate

import (
	"lib/utils"
	"lib/rabbitmq"
	"os"
	"strconv"
)

func Locate(name string) bool {
	_, err := os.Stat(name)
	return !os.IsNotExist(err)
}

func StartLocate() {
	q := rabbitmq.New(utils.RabbitServer())
	defer q.Close()
	q.Bind(utils.DataServerName())
	c := q.Comsume()

	for msg := range c {
		object, e := strconv.Unquote(string(msg.Body))
		if e != nil {
			panic(e)
		}
		if Locate(utils.ObjectRoot() + utils.ObjectPath() + object) {
			q.Send(msg.ReplyTo, utils.ServerAddr())
		}
	}
}
